﻿#include "Measure/SQLiteImplement2.h"
#include <QtSql/QSqlError>
#include <QVariant>
#include <QSqlQuery>
#include <QDateTime>
#include <QDebug>

SQLiteImplement2::SQLiteImplement2()
{
    m_database = QSqlDatabase::addDatabase("QSQLITE");
//    open(u8"D:/SQLite/PanelMeasure.db");
}

SQLiteImplement2::~SQLiteImplement2()
{
    m_database.close();
}

int SQLiteImplement2::open(QString fileName)
{
    m_database.setDatabaseName(fileName);
    if (!m_database.open()) return -1;
    return 0;
}
int SQLiteImplement2::saveData(MeasurementData *data, QString label, QDateTime t)
{
    Q_UNUSED(label);
    QString timeString = t.toString("yyyy-MM-dd hh:mm:ss");
    QSqlQuery query;
    query.prepare("INSERT INTO Measure (RecipeID, LineID, PanelID, Time) VALUES (:RecipeID, :LineID, :PanelID, :Time)");
    query.bindValue(":RecipeID", data->recipeID());
    query.bindValue(":LineID", data->lineID());
    query.bindValue(":PanelID", data->panelID());
    query.bindValue(":Time", timeString);
    query.exec();

    query.prepare(QString("SELECT * FROM Measure WHERE Time = '%1';").arg(timeString));
    if(!query.exec() ) return -2;
    if(!query.next() ) return -3;
    int measureID= query.value("MeasureID").toInt();

   // qDebug() << "MeasureID :" << measureID;

    //data->moveTo(0);
    int N = data->count();
    for(int i = 0; i < N; i++)
    {
        data->moveTo(i);
        MeasurePair &pair = data->currentMeasure();
        query.prepare("INSERT INTO MeasureData (MeasureID, SpecID, Value) VALUES (:MeasureID, :SpecID, :Value);");
        query.bindValue(":MeasureID", measureID);
        query.bindValue(":SpecID", pair.specID);
        query.bindValue(":Value", pair.value_mm);
        query.exec();
    }
//    m_database.close();
    return -0;

}
int SQLiteImplement2::saveData(MeasurementData *data, QString label)
{
    QDateTime t = QDateTime::currentDateTime();
    return saveData(data, label, t);
}

int SQLiteImplement2::loadRecipe(MeasurementData *data, QString label)
{
    data->clear();
    QSqlQuery query;

    query.prepare(QString("SELECT * FROM Recipe WHERE RecipeName = '%1';").arg(label));
    qDebug() << "query.exec : " << query.exec();
    if(!query.next()) return -2;
    int recipeID = query.value("RecipeID").toInt();
    QString recipeName = query.value("RecipeName").toString();
    QString description = query.value("Remark").toString();
    data->setRecipeName(recipeName, recipeID, description);

    query.prepare(QString("SELECT * FROM MeasureSpec WHERE RecipeID  = %1;").arg(recipeID));
    query.exec();
    while(query.next())
    {
        MeasurePair pair;
        pair.specID = query.value("SpecID").toInt();
        pair.name = query.value("MeasureName").toString();
        pair.CL =query.value("CL").toDouble();
        pair.UCL = query.value("UCL").toDouble();
        pair.LCL = query.value("LCL").toDouble();
        pair.USL = query.value("USL").toDouble();
        pair.LSL = query.value("LSL").toDouble();
        if(query.value("side").toString().startsWith("CF"))
        {
            pair.side = 1;
        }
        else
        {
            pair.side = 0;
        }

        pair.start.x_mm = query.value("STx").toDouble();
        pair.start.y_mm = query.value("STy").toDouble();
        pair.start.z_mm = query.value("STz").toDouble();
//        pair.start.scale = query.value("STARTSCALE").toDouble();
        pair.start.topLampIntensity = query.value("STLampTop").toInt();
        pair.start.bottomLampIntensity = query.value("STLampBottom").toInt();
        pair.start.dir = static_cast<MEASURE_DIR>(query.value("STDir").toInt());
        pair.start.pattern = static_cast<MEASURE_PATTERN>(query.value("STPattern").toInt());
        pair.start.exposure = query.value("STExposure").toDouble();
        pair.start.lens = query.value("STLens").toDouble();

        pair.end.x_mm = query.value("EDx").toDouble();
        pair.end.y_mm = query.value("EDy").toDouble();
        pair.end.z_mm = query.value("EDz").toDouble();
        pair.end.topLampIntensity = query.value("EDLampTop").toInt();
        pair.end.bottomLampIntensity = query.value("EDLampBottom").toInt();
        pair.end.dir = static_cast<MEASURE_DIR>(query.value("EDDir").toInt());
        pair.end.pattern = static_cast<MEASURE_PATTERN>(query.value("EDPattern").toInt());
        //pair.end.scale = query.value("ENDSCALE").toDouble();

        pair.end.exposure = query.value("EDExposure").toDouble();
        pair.end.lens = query.value("EDLens").toDouble();

        pair.stop = query.value("NeedStop").toBool();
        if(pair.stop)
        {
            pair.stopMessage = query.value("StopMessage").toString();
        }
        data->append(pair);
    }
//    m_database.close();
    return 0;
}

int SQLiteImplement2::readSpec(MeasurementData *data, int N)
{
    return 0;
}


int SQLiteImplement2::createRecipe(MeasurementData *data, QString label)
{
    return 0;
}
